<?php

class FormElement_Position extends FormElement
{

  public function beforeIndex()
  {
    $action = Simplify::request()->get('action');
    $pk = Simplify::request()->get($this->form->getPk());
    $direction = Simplify::request()->get('direction');

    if ($action == 'move' && $pk && $direction) {
      $this->form->gateway()->move($this->getField(), $pk, $direction);
    }
  }

  public function render($row, $action)
  {
    switch ($action) {
      case Form::ACTION_INDEX :
        $value = $this->getValue($row);

        $pk = $this->form->getPk();
        $id = $this->getFieldValue($row, $pk);

        $icon = Helper::factory('icon');

        $output = ''
          . '<a href="'.Simplify::response()->makeUrl(Simplify::request()->route(), 
            array('action' => 'move', $pk => $id, 'direction' => 'top')).'">&nbsp;'
          . $icon->show('top') . '</a>'
          . '<a href="'.Simplify::response()->makeUrl(Simplify::request()->route(), 
            array('action' => 'move', $pk => $id, 'direction' => 'up')).'">&nbsp;'
          . $icon->show('up') . '</a>'
          . '<a href="'.Simplify::response()->makeUrl(Simplify::request()->route(), 
            array('action' => 'move', $pk => $id, 'direction' => 'down')).'">&nbsp;'
          . $icon->show('down') . '</a>'
          . '<a href="'.Simplify::response()->makeUrl(Simplify::request()->route(), 
            array('action' => 'move', $pk => $id, 'direction' => 'bottom')).'">'
          . $icon->show('bottom') . '</a>'
        ;

        return $output;

        break;
    }

    return null;//parent::render($row, $action);
  }

}

?>